# -*- coding: UTF-8 -*-
import heapq
import json
import math
import datetime
import os
import random
import re
import threading
import time
# from cStringIO import StringIO
# region 实例1 循环格式化输出
# for i in range(1, 5, 1):
#     for j in range(1, 5, 1):
#         for k in range(1, 5, 1):
#             if (i != j) and (j != k) and (i != k):
#                 print "{}{}{}".format(i, j, k)
# endregion

# region 实例2 奖金计算
# i = float(input("净利润："))
# jiangjin = 0
# if i > 1000000:
#     jiangjin = (i - 1000000) * 0.01 + 400000 * 0.015 + 200000 * 0.03 + 200000
#     * 0.05 + 100000 * 0.075 + 100000 * 0.1
# elif i > 600000:
#     jiangjin = (i - 600000) * 0.015 + 200000 * 0.03 + 200000 * 0.05 + 100000
#     * 0.075 + 100000 * 0.1
# elif i > 400000:
#     jiangjin = (i - 400000) * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000
#     * 0.1
# elif i > 200000:
#     jiangjin = (i - 200000) * 0.05 + 100000 * 0.075 + 100000 * 0.1
# elif i > 100000:
#     jiangjin = (i - 100000) * 0.075 + 100000 * 0.1
# else:
#     jiangjin = i * 0.1
# print "奖金", jiangjin
#
# line = [1000000, 600000, 400000, 200000, 100000, 0]
# lilv = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
# r = 0
# for index in range(0, 6):
#     if i > line[index]:
#         r += (i - line[index]) * lilv[index]
#         i=line[index]
# print "奖金：",r;
# endregion

# region 实例3 数字开方
#
# for i in range(2000):
#     x = int(math.sqrt(i + 100))
#     y = int(math.sqrt(i + 268))
#     if (x * x == i + 100) and (y * y == i + 268):
#         print i
# endregion

# region 实例4 输出日期是当年的第几天
# import calendar
#
# year = input("输入年份：")
# month = input("输入月份：")
# day = input("输入日期:")
#
# months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
# sum = 0
# if 0< month<=12:
#     sum=months[month-1]+day;
# if calendar.isleap(year) and month>2:
#     sum+=1
#
# print sum
# endregion

# region 实例5 延时输出

#
# dic = {1: "1122", 2: 54, 3: 454, 4: "as"}
# for key,value in dic.items():
#     time.sleep(1);
#     print "%d :%s" %(key,value)
# endregion

# region 实例6 斐波那契数列
# def fub(n):
#     a,b=1,1
#     for i in range(n-1):
#         a,b=b,a+b
#     return a
#
# print fub(1)
# print fub(2)
# print fub(3)
# print fub(4)
# print fub(5)
# print fub(6)
# print fub(7)
# endregion

# region 实例7 寻找素数

#
#
# def getprime(end, start=2):
#     print "%d到%d之间的素数：" % (start, end)
#     s=0;
#     leep=1;
#     for i in range(start, end):
#         m = int(math.sqrt(i + 1));
#         for j in range(2,m+1):
#             if i%j==0:
#                 leep=0
#                 break
#         if leep==1:
#             s+=1
#             print i;
#         else:leep=1
#
#     print "共计素数%d个" % s;
#     return ""
#
# print getprime(200)
# print getprime(500,200)
# endregion

# region 实例8 水仙花数

#
# def narcissistic():
#     for i in range(100, 1000):
#         x = int(i / 100);
#         y = int(i / 10 % 10)
#         z = i % 10
#         if i == math.pow(x, 3) + math.pow(y, 3) + math.pow(z, 3):
#             print i


# i = narcissistic()
# endregion

# region
# for i in range(1024):
#     m = struct.pack(">i", i)
#     if m.count("\x7d")>0 :
#         t=m.encode("hex")
#         s=m.replace("\x7d","\x7d\x5d")
#
#         print i, t
# endregion

# region 继承
# class tbase(object):
#     def __init__(self):
#         self.func1()
#
#     def func1(self):
#         print "tbase.func1"
#
#
# class tclass(tbase):
#
#     def __init__(self):
#         tbase.__init__(self)
#
#     def func1(self):
#         print "tclass"
#
#
# t=tbase()
# t1=tclass()

# endregion

# region 正则 循环匹配
# line = "sdfg4wert584dfs64"
# matchObj = re.compile( '(\d+)')
# if matchObj:
#     print matchObj.findall(line)
# endregion

# region 打印树
# def show_tree(tree,total_width=36,fill=" "):
#     output=StringIO()
#     last_row=-1
#     for i,n in enumerate(tree):
#         if i:
#             row=int(math.floor(math.log(i+1,2)))
#         else:
#             row=0
#         if row!= last_row:
#             output.write("\n")
#         column=2**row
#         col_width=int(math.floor((total_width*1.0)/column))
#         output.write(str(n).center(col_width,fill))
#         last_row=row
#     print output.getvalue()
#     print "-"*total_width
#     print
#     return
#
#
# data=random.sample(range(1,10),6)
# heap=[]
# print data
# for i in data:
#     print "add %d :"%i
#     heapq.heappush(heap,i)
#     show_tree(heap)

# endregion
import struct

# d=[{"se":12,"we":87},{"se":85,"we":52}]
# print json.dumps(d)

# st=time.time()
# print st
# dt= datetime.datetime.fromtimestamp(st).date()
# print dt+datetime.timedelta(days=1)
# import requests
import requests

dicts = {"ss": 'null', "2ss": "24", "ss5": "54", }


def posttest():
    data = {
        "Command": "query",
        "SubCommand": "status",
        "DeviceID": '111'
    }
    r = requests.post("http://127.0.0.1:8080/ups_c1krsasmx/GetStatus", data=data)
    print(r.content)


def _translate_upsstatus(ups_status):
    print(ups_status)
    s = ("%08d" % int(bin(ups_status).replace("0b", "")))[::-1]
    s = bin(ups_status).replace("0b", "").zfill(8)[::-1]
    print(s)
    status_list = [
        {0: u"", 1: u""},
        {0: u"", 1: u"关机,"},
        {0: u"", 1: u"测试进行中,"},
        {0: u"在线式,", 1: u"后备式,"},
        {0: u"", 1: u"故障,"},
        {0: u"", 1: u"旁路模式,"},
        {0: u"", 1: u"电池电压低,"},
        {0: u"", 1: u"市电异常,"}]
    status = u""
    tmp = 1

    if ups_status == 2:
        return status_list[1][1].strip(",")
    for i in range(8):
        l = int(s[i]) & 1
        status += status_list[i][l]

    # for i in range(8):
    #     status += status_list[i][ups_status & tmp]
    #     tmp << 1

    return status.strip(",")


#
# list = [0, 1, 2, 4, 8, 16, 32, 64, 128]
# for i in range(2, 256):
#     print _translate_upsstatus(i)


# posttest()


# def try_exception(num):
#     try:
#         return int(num)
#     except ValueError, arg:
#         print arg, "is not a number"
#     else:
#         print "this is a number inputs"

# print time.localtime()

# strtest='16456werwsa'
# print strtest[:2]

# import sys
# print sys.argv


# urls = [("127.0.0.1", 1245, 5), ("192.168.52.1", 5426, 4), ("154.12.5.32", 4585, 14)]
# # 降幂 reverse=false
# urls.sort(key=lambda i: i[2],reverse=False)
# print urls


# listtest = []
# heapq.heappush(listtest, ("ce", "cess", 1, "se"))
# heapq.heappush(listtest, ("wer", "ce", 2, "wer"))
# sx = heapq.heappop(listtest)
# v1, v2, v3, v4 = heapq.heappop(listtest)
#
# print sx
# print v1, v2, v3, v4


# import threading
#
# global_num = 0
# lock = threading.Lock()
#
#
# def test1():
#     global global_num
#     lock.acquire()
#     for i in range(1000000):
#         global_num += 1
#     lock.release()
#     print("test1", global_num)
#
#
# def test2():
#     global global_num
#     lock.acquire()
#     for i in range(1000000):
#         global_num += 1
#     lock.release()
#     print("test2", global_num)
#
#
# t1 = threading.Thread(target=test1)
# t2 = threading.Thread(target=test2)
# t1.start()
# t2.start()


# boundary = '%s' % hex(int(time.time() * 1000))
# print boundary

# s1 = {'a', 'b', 'c', 'd'}
# s2 = {'b', 'c', 'd', 'e', 'f'}
#
# print(set(s1) - set(set(s1) & set(s2)))
# print(set(s1) & set(set(s1) ^ set(s2)))

#
# for i in range(2):
#     s = input()
#     if len(s) <= 8:
#         print('{:08}'.format(s))
#     else:
#         ss = [s[i:i + 8] for i in range(0, len(s), 8)]
#         for sss in ss:
#             print('{:08}'.format(sss))


if __name__ == '__main__':
    def test(i):
        data = {
            "Command": "query",
            "SubCommand": "status",
            "DeviceID": i
        }
        r = requests.post("http://127.0.0.1:6001/api/test3", json=data)
        print(r.content)


    ths = []
    ts = time.time()
    for i in range(10):
        t = threading.Thread(target=test, args=(i,))
        ths.append(t)
        t.start()
        print(f'{i}')
    for i in ths:
        i.join()
    print(f'cost {time.time() - ts}')
